package com.ruoyi.web.controller.project.textbook;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.system.domain.project.textbook.Textbook;
import com.ruoyi.system.domain.project.textbook.TextbookTeacher;
import com.ruoyi.system.service.project.ITextbookService;
import com.ruoyi.system.service.project.ITextbookTeacherService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 教材编写Controller
 * 
 * @author ruoyi
 * @date 2025-10-13
 */
@RestController
@RequestMapping("/project/textbook")
public class TextbookController extends BaseController
{
    @Autowired
    private ITextbookService textbookService;
    @Autowired
    private ITextbookTeacherService textbookTeacherService;
    /**
     * 查询教材编写列表
     */
    @GetMapping("/list")
    public TableDataInfo list(Textbook textbook)
    {
        startPage();
        List<Textbook> list = textbookService.selectTextbookList(textbook);
        return getDataTable(list);
    }

    /**
     * 导出教材编写列表
     */
    @PreAuthorize("@ss.hasPermi('system:textbook:export')")
    @Log(title = "教材编写", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Textbook textbook)
    {
        List<Textbook> list = textbookService.selectTextbookList(textbook);
        ExcelUtil<Textbook> util = new ExcelUtil<Textbook>(Textbook.class);
        util.exportExcel(response, list, "教材编写数据");
    }

    /**
     * 获取教材编写详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(textbookService.selectTextbookById(id));
    }

    /**
     * 新增教材编写
     */
    @Log(title = "教材编写", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Textbook textbook)
    {
        TextbookTeacher textbookTeacher = new TextbookTeacher();
        if(textbook.getUserId() != null){
            textbookTeacher.setBookId(textbook.getId());
        }else {
            textbookTeacher.setBookId(-1L);
        }
        List<TextbookTeacher> list = textbookTeacherService.selectTextbookTeacherList(textbookTeacher);
        Long userId = textbook.getUserId();
        if(list!=null&&!list.isEmpty()){
            for (TextbookTeacher textbookTeacher1 : list) {
                if(textbookTeacher1.getUserId().equals(userId)){
                    return error("此著作您已经参与并有名次记录，请勿重复添加");
                }
            }
        }
        return toAjax(textbookService.insertTextbook(textbook));
    }

    /**
     * 修改教材编写
     */
    @Log(title = "教材编写", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Textbook textbook)
    {
        return toAjax(textbookService.updateTextbook(textbook));
    }

    /**
     * 删除教材编写
     */
    @Log(title = "教材编写", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(textbookService.deleteTextbookByIds(ids));
    }

    /**
     * 根据关键字查询科研获奖信息
     */
    @GetMapping("/getPaperInfoByTitle")
    public AjaxResult getPaperInfoByTitle(  String data)
    {
        Textbook ra = textbookService.getPaperInfoByTitle(data);
        return success(ra);
    }
}
